package com.jing.ads.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.jing.ads.handler.LogHandler;
import com.jing.ads.model.PushAppInfo.AppDetail;
import com.jing.ads.net.RequestListener;
import com.jing.ads.util.GlobalUtil;

public class ActivityInstall extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent i = getIntent();
		if (i.getStringExtra("package_name") != null) {
			String pckName = i.getStringExtra("package_name");
			AppDetail app = initData(pckName);
			boolean install = i.getBooleanExtra("is_install", true);
			if (install) {
				LogHandler lh = new LogHandler(this);
		    	lh.setParams(LogHandler.ACTION_INSTALL, pckName);
		    	lh.onRequest(new RequestListener() {
					
					@Override
					public void onError(Object error) {
						
					}
					
					@Override
					public void onCallBack(Object data) {
						
					}
				}, false);
				GlobalUtil.startInstall(this, new File(app.filePath));
			}else {
				LogHandler lh = new LogHandler(this);
		    	lh.setParams(LogHandler.ACTION_ACTIVE, pckName);
		    	lh.onRequest(new RequestListener() {
					
					@Override
					public void onError(Object error) {
						
					}
					
					@Override
					public void onCallBack(Object data) {
						
					}
				}, false);
				Intent intent = GlobalUtil.getAppLaunchIntent(this.getPackageManager(), pckName);
				if (intent != null) {
					startActivity(intent);
				}
			}
			finish();
		}
	}

	private AppDetail initData(String pckName) {
		AppDetail app = new AppDetail();
		app.packageName = pckName;
		GlobalUtil.setAppDownloadStatus(this.getContentResolver(), app);
		return app;
	}
}
